package algorithm_optimal_solution.StackAndQueue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

//寻找无重复可见的山峰对数量：使用顶小底大的单调栈，创建一个类存放值及出现次数，用于栈的保存类型。
// 当破坏单调栈原则弹栈的时候，如果值只出现1次那么可见山峰对为2；如果值大于1次，那么可见山峰对为2 * times + times * (times - 1) / 2。
// 清算阶段，栈长大于2的数山峰对同上；times * (times - 1) / 2 + (栈长等于2的数如果出现次数为1 ? times : 2 * times)；栈长等于1的数如果出现次数为1山峰对为0，大于1出现次数为times * (times - 1) / 2
public class VisiblePeaksNumber {

    class Peak{
        private int num;
        private int times;

        public Peak(int num){
            this.num = num;
            this.times = 1;
        }

        public int getNum(){
            return this.num;
        }

        public int getTimes(){
            return this.times;
        }

        public void setTimes(){
            this.times++;
        }
    }

    public int nextIndex(int index,int len){
        return index < len - 1 ? index + 1 : 0;
    }

    public int getPeaks(int times){
        return times == 1 ? 2 : 2 * times + times * (times - 1) / 2;
    }

    public void getVisiblePeaksNumbers(int[] arr,int maxIndex){
        Stack<Peak> stack = new Stack<>();
        stack.push(new Peak(arr[maxIndex]));
        int len = arr.length;
        int index = nextIndex(maxIndex,len);
        int count = 0;
        while(index != maxIndex){
            while(stack.peek().getNum() < arr[index]){
                Peak peak = stack.pop();
                count += getPeaks(peak.getTimes());
            }
            if(stack.peek().getNum() == arr[index]){
                stack.peek().setTimes();
            }else{
                stack.push(new Peak(arr[index]));
            }
            index = nextIndex(index,len);
        }
        while(!stack.isEmpty()){
            if(stack.size() > 2){
                count += getPeaks(stack.pop().getTimes());
            }
            if(stack.size() == 2){
                int times = stack.pop().getTimes();
                count += times * (times - 1) / 2 + (stack.peek().getTimes() == 1 ? times : 2 * times);
            }
            if(stack.size() == 1){
                int times = stack.pop().getTimes();
                count += times == 1 ? 0 : (times * (times - 1)) / 2;
            }
        }
        System.out.println(count);
    }

    public static void main(String[] args)throws IOException{
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int len = Integer.parseInt(scanner.readLine());
        String[] values = scanner.readLine().split(" ");
        int maxIndex = 0;
        int[] arr = new int[len];
        for(int i = 0;i<len;i++){
            int value = Integer.parseInt(values[i]);
            arr[i] = value;
            maxIndex = arr[maxIndex] > value ? maxIndex : i;
        }
//        new VisiblePeaksNumber().noRepeatPeaks();
        new VisiblePeaksNumber().getVisiblePeaksNumbers(arr,maxIndex);
    }

    //寻找无重复可见的山峰对数量：除了最大值和次大值外，还剩i-2个节点，这i-2个节点都根据小找大的方式，每一个都能找到两对，共有（i-2）*2对。
    // 还有1对，就是次大值看见最大值这对。所以一共是2*i-3对。
    public void noRepeatPeaks()throws IOException{
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int len = Integer.parseInt(scanner.readLine());
        for(int i = 0;i<len;i++){
            String[] value = scanner.readLine().split(" ");
            int number = Integer.parseInt(value[0]);
            if(number == 1){
                System.out.println(0);
            }else{
                System.out.println(2 * (number - 2) + 1);
            }
        }
    }
}
